package a2;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openrdf.repository.Repository;
import org.openrdf.repository.sparql.SPARQLRepository;

public class Main {
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out
					.println("arguments: [repository, see below] [queryFile]");
			System.out.println("\t\ta=Assignment2, k=KnowRob");
			System.exit(0);
		}

		if (!(args[0].equals("a") || args[0].equals("k"))) {
			System.out
					.println("Invalid repository argument, please use this format:");
			System.out
					.println("arguments: [repository, see below] [queryFile]");
			System.out.println("\t\ta=Assignment2, k=KnowRob");
		}

		try {
			Repository repo;
			if (args[0].equals("a")) {
				repo = new SPARQLRepository(
						"http://vm15.cs.lth.se:8085/openrdf-sesame/repositories/Assignment2");
			} else {
				repo = new SPARQLRepository(
						"http://vm15.cs.lth.se:8085/openrdf-sesame/repositories/KnowRob");
			}
			String fileName = args[1];
			String queryString = FileUtils.readFileToString(new File(fileName));
			String resultString = SPARQLQueryHandler.performQuery(queryString,repo);
			
			System.out.println("------------------------------------------");
			System.out.println("Query was successful. Here is the result:");
			System.out.println(resultString);
			System.exit(0);
		} catch (IOException e) {
			System.out.println("IOException...");
			e.printStackTrace();
		}
	}
}
